package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysRetail;
import com.ruoyi.system.domain.SysRetailProduct;
import com.ruoyi.system.domain.SysRetailTask;
import com.ruoyi.system.mapper.SysRetailMapper;
import com.ruoyi.system.mapper.SysRetailProductMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.service.ISysOrderService;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-24
 */
@Service
public class SysOrderServiceImpl implements ISysOrderService
{
    @Autowired
    private SysOrderMapper sysOrderMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRetailProductMapper sysRetailProductMapper;
    @Autowired
    private SysRetailMapper sysRetailMapper;
    @Autowired
    private SysRetailTaskServiceImpl sysRetailTaskService;

    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public SysOrder selectSysOrderByOrderId(Long orderId)
    {
        return sysOrderMapper.selectSysOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param sysOrder 订单
     * @return 订单
     */
    @Override
    public List<SysOrder> selectSysOrderList(SysOrder sysOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = sysUserMapper.selectUserById(loginUser.getUserId());
        sysOrder.setSupplierId(user.getSupplierId());
        return sysOrderMapper.selectSysOrderList(sysOrder);
    }

    /**
     * 新增订单
     *
     * @param sysOrder 订单
     * @return 结果
     */
    @Override
    public int insertSysOrder(SysOrder sysOrder)
    {
        return sysOrderMapper.insertSysOrder(sysOrder);
    }

    /**
     * 修改订单
     *
     * @param sysOrder 订单
     * @return 结果
     */
    @Override
    public int updateSysOrder(SysOrder sysOrder)
    {
        sysOrder.setProductionSchedule("1");
        SysRetailTask sysRetailTask = new SysRetailTask();
        sysRetailTask.setoId(sysOrder.getOrderId());
        System.out.println(sysOrder.getRetailList());
        sysRetailTask.setrId(sysOrder.getRetailList());
        sysRetailTask.setTaskQuantity(sysOrder.getTaskQuantity());
        sysRetailTaskService.insertSysRetailTask(sysRetailTask);
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByOrderIds(Long[] orderIds)
    {
        return sysOrderMapper.deleteSysOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByOrderId(Long orderId)
    {
        return sysOrderMapper.deleteSysOrderByOrderId(orderId);
    }

    @Override
    public List<SysRetail> selectRetailListByProductId(Long proId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = sysUserMapper.selectUserById(loginUser.getUserId());
        SysRetailProduct sysRetailProduct = new SysRetailProduct();
        sysRetailProduct.setProId(proId);
        List<SysRetail> sysRetails = new ArrayList<>();
        List<SysRetailProduct> sysRetailProducts = sysRetailProductMapper.selectSysRetailProductList(sysRetailProduct);
        for (SysRetailProduct retailProduct : sysRetailProducts) {
            SysRetail sysRetail = sysRetailMapper.selectSysRetailByRId(retailProduct.getrId());
            if (sysRetail.getSupplierId().equals(user.getSupplierId())) {
                sysRetails.add(sysRetail);
            }
        }
        return sysRetails;
    }
}
